// pages/challenge/challenge.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    num: 0,
    name: "",
    explain: "",
    grade: 0,
    isFocus:false,
    disabled:true,
    array:[]
  },

  toResult() {
    // console.log("ok")
    wx.redirectTo({
      url: '../result/result'
    })
  },

  addClickcorrect:function(){
    // console.log("点击成功")
    var tempFilePath ='http://www.linjianyi.link/audio/correct.mp3' ;//音频文件地址
    let myaudio = wx.createInnerAudioContext(); // 创建audio对象
    myaudio.onError((res) => {
      // 播放音频失败的回调
      // console.log(res)
    })
  myaudio.src = tempFilePath;  // 这里可以是录音的临时路径
  myaudio.play();
},

addClickwrong:function(){
  // console.log("点击失败")
  var tempFilePath ='http://www.linjianyi.link/audio/wrong.mp3' ;//音频文件地址
  let myaudio = wx.createInnerAudioContext(); // 创建audio对象
  myaudio.onError((res) => {
    // 播放音频失败的回调
    // console.log(res)
  })
myaudio.src = tempFilePath;  // 这里可以是录音的临时路径
myaudio.play();

  // // innerAudioContext.src = ; // 音频资源的地址
  // innerAudioContext.onPlay()
},

  nextt(options) {
    let that=this
    let grade1 = this.data.grade
    let num1 = this.data.num
    if(num1==10){
      let array=JSON.stringify(this.data.array) 
      // console.log(array)
      wx.reLaunch({
        url: '/pages/result/result?grade='+that.data.grade+'&array='+array
        
      })
    }
    //用户点击了正确答案
    if(this.data.disabled){
    if (options.currentTarget.dataset.type ==this.data.explain) {
      this.setData({
        grade: grade1 + 10,
      }),
      this.addClickcorrect(),
      //并且再次获取新的数据
      wx.request({
        url: 'https://api.tianapi.com/anslajifenlei/index',
        data: {
          key: "fd8e7e6160708c75e03c0529cef9096f"
        },
        success(res) {
          // console.log(res)
          that.setData({
            name: res.data.newslist[0].name,
            explain: res.data.newslist[0].explain,
            num: num1 + 1
          })
        }
      })
    }
    //用户点击错误
    if(options.currentTarget.dataset.type !==this.data.explain){
      this.setData({
        isFocus:true,
        disabled:false
      })
      this.addClickwrong()
    }
    //添加数据
this.data.array.push({'name':this.data.name,'explain':this.data.explain})
  }
  },
  toNext(){
    let num1 = this.data.num
    this.setData({
      isFocus:false,
      num: num1 + 1,
      disabled:true
    })
    let that=this
    //并且再次获取新的数据
    wx.request({
      url: 'https://api.tianapi.com/anslajifenlei/index',
      data: {
        key: "fd8e7e6160708c75e03c0529cef9096f"
      },
      success(res) {
        // console.log(res)
        // that.name=
        that.setData({
          name: res.data.newslist[0].name,
          explain: res.data.newslist[0].explain
        })
        
      }
    })
  },
  onShow() {
    let that = this
    wx.request({
      url: 'https://api.tianapi.com/anslajifenlei/index',
      data: {
        key: "fd8e7e6160708c75e03c0529cef9096f"
      },
      success(res) {
        // console.log(res)
        // that.name=
        that.setData({
          name: res.data.newslist[0].name,
          explain: res.data.newslist[0].explain
        })
      }
    })
  }
})